<?php

/* *
 * 功能：支付宝服务器异步通知页面
 * 版本：3.3
 * 日期：2012-07-23
 * 说明：
 * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
 * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。


 * ************************页面功能说明*************************
 * 创建该页面文件时，请留心该页面文件中无任何HTML代码及空格。
 * 该页面不能在本机电脑测试，请到服务器上做测试。请确保外部可以访问该页面。
 * 该页面调试工具请使用写文本函数logResult，该函数已被默认关闭，见alipay_notify_class.php中的函数verifyNotify
 * 如果没有收到该页面返回的 success 信息，支付宝会在24小时内按一定的时间策略重发通知
 */

require_once("alipay.config.php");
require_once("lib/alipay_notify.class.php");


$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyNotify();
//$verify_result = 1;

if ($verify_result == 1) {

    //验证成功

    $dsn = 'mysql:host=localhost;dbname=kkmai';
    $username = 'root';
    $passwd = 'eDKmpHzrfkcKrHavT5NLuQ';

    $pdo = new \PDO($dsn, $username, $passwd);
    $pdo->exec('set names utf8');

    //批次号
    $batch_no = $_REQUEST['batch_no'];
    $success_details = $_POST['success_details'];
    $fail_details = $_POST['fail_details'];

//    $batch_no = '220210114161059633512';
//    $success_details = '成功了';
//    $fail_details = '失败了';

    if ($success_details) {
        $sql = "SELECT * FROM ai_staff_tixian WHERE batch = :batch LIMIT 0, 1";
        $stat = $pdo->prepare($sql);
        $result = $stat->execute([':batch' => $batch_no]);
        if ($result) {
            $row = $stat->fetch(\PDO::FETCH_ASSOC);
            if ($row && $row['status'] == 1) {
                $updateSql = "UPDATE ai_staff_tixian SET status = :status, utime = :utime WHERE batch = :batch";
                $updateStat = $pdo->prepare($updateSql);
                $updateResult = $updateStat->execute([
                    ':status' => 2,
                    ':utime' => date('Y-m-d H:i:s'),
                    ':batch' => $batch_no,
                ]);

                //更新美容师流水
                $staffFlowRowsql = "SELECT * FROM `ai_staff_flow` WHERE `staff_id`= :staff_id ORDER BY `id` DESC LIMIT 0,1";
                $staffFlowRowstat = $pdo->prepare($staffFlowRowsql);
                $staffFlowRowresult = $staffFlowRowstat->execute([':staff_id' => $row['staff_id']]);
                $staffFlowRow = $staffFlowRowstat->fetch(\PDO::FETCH_ASSOC);
                if ($staffFlowRow) {
                    $last_money = $staffFlowRow['last_money'];
                } else {
                    $last_money = 0;
                }

                $insertStaffFlowsql = "INSERT INTO `ai_staff_flow_new`(`order_id`,`item_name`, `type`, `pro_type`, `staff_id`, `all_money`, `money`, `status`, `add_time`) VALUES (:order_id,'提现',2,8,:staff_id,:all_money,:money,2,:add_time)";
                $insertStaffFlowstat = $pdo->prepare($insertStaffFlowsql);
                $insertStaffFlowresult = $insertStaffFlowstat->execute([
                    ':order_id' => $row['id'],
                    ':staff_id' => $row['staff_id'],
                    ':all_money' => $row['money'] + $row['poundage'],
                    ':money' => $row['money'] + $row['poundage'],
                    ':add_time' => date('Y-m-d H:i:s'),
                ]);
                if ($updateResult && $insertStaffFlowresult) {
                    $staffsql = "SELECT staff_name,telephone,money,tx_money FROM ai_staff WHERE id = :id LIMIT 0, 1";
                    $staffstat = $pdo->prepare($staffsql);
                    $result = $staffstat->execute([':id' => $row['staff_id']]);
                    $staffRow = $staffstat->fetch(\PDO::FETCH_ASSOC);

                    $ApiPath = 'http://ai.app.dwkkm.com/';
                    $staffName = $staffRow['staff_name'];
                    $tixianMoney = $row['money'] / 100;
                    $money = $staffRow['money'] / 100;
                    $all = ($staffRow['money'] + $staffRow['tx_money']) / 100;
                    $tx_money = $staffRow['tx_money'] / 100;

                    $postFields['content'] = '【提现成功】尊敬的' . $staffName . '，您的提现金额' . $tixianMoney . '已到账，请及时查收，您的账户余额' . $money . '，您总计营收' . $all . '，已提现完成总计' . $tx_money;
                    $postFields['type'] = 7; //0新店上新7提现
                    $postFields['titleId'] = 3; //1店铺动态2预约消息3营收消息
                    $postFields['otherId'] = 0; //新店上新就是0
                    $postFields['staffId'] = $row['staff_id'];
                    $postFields['storeId'] = 0;
                    $postFields['phone'] = $staffRow['telephone'];
                    $url = $ApiPath . 'push/pushMessage/';
                    $headers = array(
                        'Content-type:application/json;charset=utf-8',
                        'Accept:application/json',
                        'Cache-Control: no-cache',
                        'Pragma: no-cache'
                    );
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields));
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                    $result = curl_exec($ch);
                    curl_close($ch);
//                    Db::commit();
                    echo 'success';
                } else {
//                    Db::rollback();
                    echo 'fail1';
                }
            } else {
                echo 'fail2';
            }
        } else {
            echo 'fail3';
        }
    } else {
        echo $fail_details;
    }
} else {
    echo "fail4";
}
?>
